const { logger } = require('../utils');
const { knex, TABLE_NAME } = require('./db');

knex.schema.hasTable(TABLE_NAME.tenant).then((exists) => {
  if (!exists) {
    return knex.schema.createTable(TABLE_NAME.tenant, (t) => {
      t.charset('utf8');
      t.uuid('id').primary();
      t.string('name', 100);
      t.json('enterpriseOperator');
      t.string('address');
      t.string('flowIds');
      t.foreign('flowIds').references(`${TABLE_NAME.contract}.id`)
        .onDelete('SET NULL');
      t.string('organizationId', 50);
      t.string('templateId', 50);
      t.timestamp('createdOn').defaultTo(knex.fn.now());
      t.timestamp('contractEndDate').defaultTo(knex.fn.now());
      t.string('latestFlowStatus');
    });
  }
})
  .catch((err) => {
    logger.error({
      message: err.message,
      stack: err.stack,
      Action: 'tenant/createTable',
    });
    console.error(err);
  });


